﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.fminutes.vinciDll {
    import flash.events.*;
    import flash.utils.*;
    import flash.display.*;
    import com.fminutes.vinciDll.analyze.*;
    import flash.system.*;

    public class Dll extends EventDispatcher {

        private static var instance:Dll;

        public var mainAppDomain:ApplicationDomain;
        public var floder:String;
        public var outFloder:String;
        public var lang:String = "cn";
        public var status:String = "";
        public var fileList:FileList;
        private var loadingLoader:DllLoader;
        private var mainLoader:DllLoader;
        private var listTimer:Timer;
        private var delayLoader:DllLoader;
        private var delayFuncDic:Dictionary;
        private var currentDllLoader:DllLoader;
        private var dllFileLoader:DllLoader;
        private var lisLoader:DllLoader;
        private var dddAnalyze:DDDAnalyze;
        private var libAnalyze:LibAnalyze;
        private var dllRequestDic:Array;
        private var appDomain:ApplicationDomain;

        public function Dll(){
            this.listTimer = new Timer(10);
            this.delayFuncDic = new Dictionary();
            this.dddAnalyze = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_DDD) as DDDAnalyze);
            this.libAnalyze = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIB) as LibAnalyze);
            this.dllRequestDic = [];
            super();
            if (instance == null){
                instance = this;
            } else {
                throw (new Error("实例化单例类出错-Dll"));
            };
        }
        public static function getInstance():Dll{
            if (instance == null){
                instance = new (Dll)();
            };
            return (instance);
        }

        public function loadINIXML(_arg1:Array, _arg2:String="", _arg3:String=""):void{
            var _local4:IniToFlieList = new IniToFlieList();
            _local4.analyzeXml(_arg1, this.lang, this.status, this.loadINIXMLComp, _arg2, _arg3);
        }
        private function loadINIXMLComp(_arg1:FileList):void{
            this.fileList = _arg1;
            this.startLoad();
        }
        private function startLoad():void{
            this.loadLoadingList();
        }
        public function loadLoadingList():void{
            this.loadingLoader = new DllLoader();
            this.currentDllLoader = this.loadingLoader;
            this.loadingLoader.load(this.fileList.loadingList);
            this.loadingLoader.addEventListener(MultiLoaderEvent.COMPLETE, this.loadingComplete);
            this.loadingLoader.addEventListener(MultiLoaderEvent.PROGRESS, this.loadingProgress);
        }
        private function loadingProgress(_arg1:MultiLoaderEvent):void{
            var _local2:DllEvent = new DllEvent(DllEvent.PROGRESS, _arg1.data);
            dispatchEvent(_local2);
        }
        private function loadingComplete(_arg1:MultiLoaderEvent):void{
            var _local2:Dll = Dll.getInstance();
            Dll.getInstance().dispatchEvent(new DllEvent(DllEvent.LOADING_COMPLETE));
            this.loadMainList();
            this.loadingLoader.removeEventListener(MultiLoaderEvent.COMPLETE, this.loadingComplete);
            this.loadingLoader.removeEventListener(MultiLoaderEvent.PROGRESS, this.loadingProgress);
            this.loadingLoader = null;
        }
        public function loadMainList():void{
            this.mainLoader = new DllLoader();
            this.currentDllLoader = this.mainLoader;
            this.mainLoader.load(this.fileList.mainList);
            this.mainLoader.addEventListener(MultiLoaderEvent.COMPLETE, this.mainComplete);
            this.mainLoader.addEventListener(MultiLoaderEvent.PROGRESS, this.mainProgress);
        }
        private function mainProgress(_arg1:MultiLoaderEvent):void{
            var _local2:DllEvent = new DllEvent(DllEvent.PROGRESS, _arg1.data);
            dispatchEvent(_local2);
        }
        private function mainComplete(_arg1:MultiLoaderEvent):void{
            Dll.getInstance().dispatchEvent(new DllEvent(DllEvent.COMPLETE));
            this.loadDelayedList();
            this.mainLoader.removeEventListener(MultiLoaderEvent.COMPLETE, this.mainComplete);
            this.mainLoader = null;
            this.listTimer.addEventListener(TimerEvent.TIMER, this.onEnterFrameHandler);
            this.listTimer.start();
        }
        public function loadDelayedList():void{
            if (!this.fileList.delayedList.length){
                return;
            };
            this.delayLoader = new DllLoader();
            this.currentDllLoader = this.delayLoader;
            this.delayLoader.load(this.fileList.delayedList);
            this.delayLoader.addEventListener(MultiLoaderEvent.COMPLETE, this.delayLoaderComplete);
        }
        private function delayLoaderComplete(_arg1:MultiLoaderEvent):void{
            var _local2:String;
            var _local3:DisplayObject;
            var _local4:LibAnalyze;
            var _local5:Function;
            for (_local2 in this.delayFuncDic) {
                _local4 = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIB) as LibAnalyze);
                _local3 = (_local4.getDisplayObjectByName(_local2) as DisplayObject);
                if (_local3){
                    _local5 = this.delayFuncDic[_local2];
                    _local5(_local3);
                    delete this.delayFuncDic[_local2];
                };
            };
        }
        public function getDelayMaterialByClassName(_arg1:String, _arg2:Function):void{
            var _local3:DisplayObject;
            this.delayFuncDic[_arg1] = _arg2;
            var _local4:LibAnalyze = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIB) as LibAnalyze);
            _local3 = (_local4.getDisplayObjectByName(_arg1) as DisplayObject);
            if (_local3){
                _arg2(_local3);
                delete this.delayFuncDic[_arg1];
            };
        }
        public function loadGroupList(_arg1:String):void{
        }
        public function addLoaderList(_arg1:Array):void{
            var _local4:DllFileObject;
            var _local2:Array = [];
            var _local3:int;
            while (_local3 < _arg1.length) {
                _local4 = new DllFileObject(_arg1[_local3].url, _arg1[_local3].name, 0, "", _arg1[_local3].type);
                _local2.push(_local4);
                _local3++;
            };
            this.currentDllLoader.addLoaderList(_local2);
        }
        public function loadDllFileObject(_arg1:Array):void{
            this.dllFileLoader = new DllLoader();
            this.dllFileLoader.load(_arg1);
        }
        public function loadLisFiles(_arg1:Array):void{
            var _local4:DllFileObject;
            this.lisLoader = new DllLoader();
            var _local2:Array = [];
            var _local3:int;
            while (_local3 < _arg1.length) {
                _local4 = new DllFileObject();
                _local4.url = _arg1[_local3].url;
                _local4.name = _arg1[_local3].name;
                _local2.push(_local4);
                _local3++;
            };
            this.lisLoader.load(_local2);
        }
        public function loadFilesByServerId(_arg1:Array, _arg2:Function=null):void{
            var _local9:Array;
            var _local10:DllFileObject;
            var _local11:DllLoader;
            var _local3:DDDAnalyze = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_DDD) as DDDAnalyze);
            var _local4:Array = [];
            var _local5:Array = [];
            var _local6:int;
            while (_local6 < _arg1.length) {
                _local4 = _local4.concat(_local3.getDataByServerId(_arg1[_local6]));
                _local6++;
            };
            var _local7:LisAnalyze = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIS) as LisAnalyze);
            var _local8:int;
            while (_local8 < _local4.length) {
                _local9 = _local7.getLisDataByMid(_local4[_local8].mid);
                if (_local9){
                } else {
                    _local10 = new DllFileObject((IniToFlieList.outFloder + _local4[_local8].swfurl), _local4[_local8].typeName, 0, "", DllFileObject.TYPE_LIS);
                    _local5.push(_local10);
                };
                _local8++;
            };
            if (_local5.length){
                _local11 = new DllLoader();
                _local11.load(_local5);
                if (_arg2 != null){
                    _local11.addEventListener(MultiLoaderEvent.PROGRESS, _arg2);
                };
            };
        }
        public function getMaterialClassByServerId(_arg1:int, _arg2:String, _arg3:Function, _arg4:Boolean=false, _arg5:Boolean=false, _arg6:String=""):void{
            var _local7:Object = new Object();
            _local7["serverId"] = _arg1;
            _local7["materialClassName"] = _arg2;
            _local7["completeFunc"] = _arg3;
            _local7["typeFlag"] = _arg4;
            _local7["loadFlag"] = _arg5;
            _local7["subClass"] = _arg6;
            this.dllRequestDic.push(_local7);
        }
        private function onEnterFrameHandler(_arg1:TimerEvent):void{
            if (this.dllRequestDic.length == 0){
                return;
            };
            var _local2:Array = this.dllRequestDic.concat();
            this.dllRequestDic.length = 0;
            this.dealLoadList(_local2);
        }
        private function dealLoadList(_arg1:Array):void{
            var _local2:int;
            var _local3:int = _arg1.length;
            _local2 = 0;
            while (_local2 < _local3) {
                this.startLoadList(_arg1[_local2]);
                _local2++;
            };
        }
        private function startLoadList(_arg1:Object):void{
            var _local9:Object;
            var _local12:Boolean;
            var _local13:int;
            var _local16:int;
            var _local18:DllFileObject;
            var _local19:DllFileObject;
            var _local20:DllLoader;
            var _local2:int = _arg1["serverId"];
            var _local3:String = _arg1["materialClassName"];
            var _local4:Function = _arg1["completeFunc"];
            var _local5:Boolean = _arg1["typeFlag"];
            var _local6:Boolean = _arg1["loadFlag"];
            var _local7:String = _arg1["subClass"];
            var _local8:Class = this.libAnalyze.getClassByName(_local3);
            if (_local8){
                if (_local5){
                    _local4(_local8);
                } else {
                    _local4(new (_local8)());
                };
                return;
            };
            if (_local7 == ""){
                _local9 = this.dddAnalyze.getMaterialDataByServerId(String(_local2));
            } else {
                _local9 = this.dddAnalyze.getMaterialDataByServerIdAndSunClass(String(_local2), _local7);
            };
            var _local10:DllFileObject = new DllFileObject((this.outFloder + _local9["swfurl"]), _local9["swfurl"], 0, "", DllFileObject.TYPE_LIB);
            _local10.subClass = _local7;
            _local10.serverid = _local2.toString();
            LisDictionary.getInstance().registerMaterialClassDic(_local2.toString(), _local3, _local4, _local5, _local7);
            var _local11:Array = LisDictionary.getInstance().lisDllFileObjct;
            if (_local11.length == 0){
                _local11.push(_local10);
            };
            var _local14:int = _local11.length;
            _local13 = 0;
            while (_local13 < _local14) {
                _local18 = _local11[_local13];
                if (_local18.serverid == _local10.serverid){
                    if (_local7 == ""){
                        _local12 = true;
                    };
                    break;
                };
                _local13++;
            };
            if (!_local12){
                _local11.push(_local10);
            };
            var _local15:Array = LisDictionary.getInstance().lisDllFileObjct;
            var _local17:int = _local15.length;
            _local16 = 0;
            while (_local16 < _local17) {
                _local19 = _local15[_local16];
                if (int(_local19.serverid) == _local2){
                    if ((((_local19.subClass == _local7)) && ((_local19.status == DllFileObject.NOT_LOADED)))){
                        _local19.status = DllFileObject.LOADING;
                        _local20 = new DllLoader();
                        _local20.load([_local19]);
                        _local20.addEventListener(MultiLoaderEvent.COMPLETE, this.onComplete);
                        break;
                    };
                };
                _local16++;
            };
        }
        public function openLoad():void{
            var _local3:DllFileObject;
            var _local4:DllLoader;
            var _local1:Array = LisDictionary.getInstance().lisDllFileObjct;
            var _local2:int;
            while (_local2 < _local1.length) {
                _local3 = _local1[_local2];
                if (_local3.status == DllFileObject.NOT_LOADED){
                    _local3.status = DllFileObject.LOADING;
                    _local4 = new DllLoader();
                    _local4.load([_local3]);
                    _local4.addEventListener(MultiLoaderEvent.COMPLETE, this.onComplete);
                    break;
                };
                _local2++;
            };
        }
        private function onComplete(_arg1:MultiLoaderEvent):void{
            var _local2:Class;
            var _local8:String;
            var _local9:Function;
            var _local10:Boolean;
            var _local11:Function;
            _arg1.target.removeEventListener(MultiLoaderEvent.COMPLETE, this.onComplete);
            this.appDomain = (_arg1.data[0].result["appDomain"] as ApplicationDomain);
            var _local3:* = _arg1.data[0].serverid;
            var _local4:String = _arg1.data[0].subClass;
            var _local5:Array = LisDictionary.getInstance().classListDic[(_local3 + _local4)];
            if (_local5 == null){
                return;
            };
            var _local6:int;
            while (_local6 < _local5.length) {
                _local8 = _local5[_local6]["className"];
                if (this.appDomain.hasDefinition(_local8)){
                    _local2 = (this.appDomain.getDefinition(_local8) as Class);
                };
                if (!_local2){
                } else {
                    _local9 = _local5[_local6]["completeFunc"];
                    _local10 = _local5[_local6]["typeFlag"];
                    if (_local10){
                        if (_local9 != null){
                            _local9(_local2);
                        };
                    } else {
                        if (_local9 != null){
                            _local9(new (_local2)());
                        };
                    };
                    _local5[_local6]["completeFunc"] = null;
                };
                _local6++;
            };
            var _local7:int;
            while (_local7 < _local5.length) {
                _local11 = _local5[_local7]["completeFunc"];
                if (_local11 == null){
                    _local5.splice(_local7, 1);
                    break;
                };
                _local7++;
            };
        }
        public function cancleGetMaterial(_arg1:int, _arg2:Function):void{
            var _local5:Function;
            var _local3:Array = LisDictionary.getInstance().classListDic[String(_arg1)];
            if (!_local3){
                return;
            };
            var _local4:int;
            while (_local4 < _local3.length) {
                _local5 = _local3[_local4]["completeFunc"];
                if (_local5 == _arg2){
                    _local3[_local4]["completeFunc"] = null;
                    break;
                };
                _local4++;
            };
        }
        public function getDataByServerId(_arg1:String, _arg2:Function):void{
            var completeArray:* = null;
            var loadArray:* = null;
            var lisComplete:* = null;
            var lisLoaded:* = null;
            var lisData:* = null;
            var dllFileObject:* = null;
            var lis:* = 0;
            var tempDllFileObject:* = null;
            var dicFuncArray:* = null;
            var dic:* = 0;
            var dllLoader:* = null;
            var serverId:* = _arg1;
            var completeFunc:* = _arg2;
            lisComplete = function (_arg1:MultiLoaderEvent):void{
                var _local2:int;
                var _local4:int;
                while (_local2 < _arg1.data.length) {
                    completeArray = completeArray.concat(_arg1.data[_local2].result);
                    _local2++;
                };
                if (completeArray.length != loadArray.length){
                    return;
                };
                var _local3:Array = LisDictionary.getInstance().listDic[serverId];
                if (_local3){
                    _local4 = 0;
                    while (_local4 < _local3.length) {
                        var _local5 = _local3;
                        _local5[_local4](completeArray);
                        _local4++;
                    };
                    delete LisDictionary.getInstance().listDic[serverId];
                };
            };
            lisLoaded = function (_arg1:Event):void{
                var _local3:int;
                if (completeArray.length != loadArray.length){
                    return;
                };
                completeArray = completeArray.concat((_arg1.target as DllFileObject).result);
                var _local2:Array = LisDictionary.getInstance().listDic[serverId];
                if (_local2){
                    _local3 = 0;
                    while (_local3 < _local2.length) {
                        var _local4 = _local2;
                        _local4[_local3](completeArray);
                        _local3++;
                    };
                    delete LisDictionary.getInstance().listDic[serverId];
                };
            };
            LisDictionary.getInstance().registerDic(serverId, completeFunc);
            completeArray = [];
            var delayLoadArray:* = [];
            var dddAnalyze:* = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_DDD) as DDDAnalyze);
            loadArray = dddAnalyze.getDataByServerId(serverId);
            var lisAnalyze:* = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIS) as LisAnalyze);
            var i:* = 0;
            while (i < loadArray.length) {
                lisData = lisAnalyze.getLisDataByMid(loadArray[i].mid);
                if (lisData){
                    completeArray = completeArray.concat(lisData);
                } else {
                    dllFileObject = new DllFileObject((IniToFlieList.outFloder + loadArray[i].swfurl), loadArray[i].typeName, 0, "", DllFileObject.TYPE_LIS);
                    if (LisDictionary.getInstance().lisDllFileObjct.length != 0){
                        lis = 0;
                        while (lis < LisDictionary.getInstance().lisDllFileObjct.length) {
                            tempDllFileObject = LisDictionary.getInstance().lisDllFileObjct[lis];
                            if (((((tempDllFileObject) && ((tempDllFileObject.url == dllFileObject.url)))) && ((tempDllFileObject.status == DllFileObject.LOADING)))){
                                tempDllFileObject.addEventListener(DllEvent.SWFURL_COMPLETE, lisLoaded);
                            } else {
                                delayLoadArray.push(dllFileObject);
                                LisDictionary.getInstance().lisDllFileObjct.push(dllFileObject);
                            };
                            lis = (lis + 1);
                        };
                    } else {
                        delayLoadArray.push(dllFileObject);
                        LisDictionary.getInstance().lisDllFileObjct.push(dllFileObject);
                    };
                };
                i = (i + 1);
            };
            if (completeArray.length == loadArray.length){
                dicFuncArray = LisDictionary.getInstance().listDic[serverId];
                dic = 0;
                while (dic < dicFuncArray.length) {
                    var _local4 = dicFuncArray;
                    _local4[dic](completeArray);
                    dic = (dic + 1);
                };
                delete LisDictionary.getInstance().listDic[serverId];
                return;
            };
            if (delayLoadArray.length){
                dllLoader = new DllLoader();
                dllLoader.load(delayLoadArray);
                dllLoader.addEventListener(MultiLoaderEvent.COMPLETE, lisComplete);
            };
        }
        public function getlisClassByName(_arg1:Array, _arg2:String):Class{
            var _local3:Class;
            var _local5:ApplicationDomain;
            var _local4:int;
            while (_local4 < _arg1.length) {
                _local5 = _arg1[_local4].appDomain;
                if (_local5.hasDefinition(_arg2)){
                    _local3 = (_local5.getDefinition(_arg2) as Class);
                    return (_local3);
                };
                _local4++;
            };
            return (null);
        }
        public function disposeGetDataByServerId(_arg1:String, _arg2:Function):void{
            var _local4:int;
            var _local3:Array = LisDictionary.getInstance().listDic[_arg1];
            if (_local3){
                _local4 = _local3.indexOf(_arg2);
                if (_local4 != -1){
                    _local3.splice(_local4, 1);
                };
            };
        }
        public function getJsonDataByName(_arg1:String):Object{
            var _local2:Object = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_JSON) as JsonAnalyze).getJsonObjectByName(_arg1);
            return (_local2);
        }
        public function getXMLDataByName(_arg1:String):XML{
            var _local2:XML = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_XML) as XMLAnalyze).getXMLByName(_arg1);
            return (_local2);
        }
        public function getDDDByName(_arg1:String):Object{
            var _local2:Object = (AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_DDD) as DDDAnalyze).getDDDDataByName(_arg1);
            return (_local2);
        }
        public function getBinObjectByName(_arg1:String):ByteArray{
            return (null);
        }
        public function getByteArrayByName(_arg1:String):ByteArray{
            var _local2:ByteArray = ((AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIB) as LibAnalyze).getByteArrayByName(_arg1) as ByteArray);
            return (_local2);
        }
        public function getClassByName(_arg1:String):Class{
            var _local2:Class = ((AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIB) as LibAnalyze).getClassByName(_arg1) as Class);
            return (_local2);
        }
        public function getDisplayObjectByName(_arg1:String):Object{
            var _local2:Object = ((AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIB) as LibAnalyze).getDisplayObjectByName(_arg1) as Object);
            return (_local2);
        }
        public function getLoaderByName(_arg1:String):Loader{
            return ((AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_MAIN) as MainAnalyze).getLoaderByName(_arg1));
        }
        public function getMaterialListByMid(_arg1:String):Array{
            return ((AnalyzeDictionary.getInstance().getAnalyze(DllFileObject.TYPE_LIS) as LisAnalyze).getMaterialByMid(_arg1));
        }

    }
}//package com.fminutes.vinciDll 
